//
//  XLPhotoCell.m
//  XLCollectionLayout
//
//  Created by yuanxiaolong on 16/3/30.
//  Copyright © 2016年 yuanxiaolong. All rights reserved.
//

#import "XLPhotoCell.h"

@interface XLPhotoCell ()

@property (nonatomic, strong)UIView *coverView;
@property (strong, nonatomic)  UIImageView *imageView;

/**
 姓名
 */
@property (nonatomic, strong) UILabel *nameLab;
/**
 年龄
 */
@property (nonatomic, strong) UIButton *ageBtn;
/**
 星座
 */
@property (nonatomic, strong) UILabel *constellationLab;
/**
 地址
 */
@property (nonatomic, strong) UILabel *address;
/**
 标志图
 */
@property (nonatomic, strong) UIImageView *addressImg;
@property (nonatomic, strong) UIButton *deteleBtn;
@property (nonatomic, strong) UIButton *messageBtn;

@end

@implementation XLPhotoCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:  self.contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight |UIRectCornerBottomRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame =   self.contentView.bounds;
        maskLayer.path = maskPath.CGPath;
        self.contentView.layer.mask = maskLayer;
        
        self.imageView = [[UIImageView alloc] init];
        
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.imageView.userInteractionEnabled = YES;

        [self.contentView addSubview:_imageView];
      

        
        self.coverView = [[UIView alloc] init];
        self.coverView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.coverView];

        CGFloat NameFont ;
        if (iPhone6p) {
            NameFont = 20.0;
        }else if (iPhone6){
            NameFont = 18.0;
        }else if(iPhone5){
            NameFont = 17.0;
        }else{
            NameFont = 20.0;

        }
        
        _nameLab = [[UILabel alloc] init];
        _nameLab.font = FontP(NameFont);
        _nameLab.textColor = [UIColor blackColor];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        [_coverView addSubview:_nameLab];
        
        
        _ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.ageBtn.enabled = NO;
        self.ageBtn.layer.cornerRadius = 4;
        self.ageBtn.layer.masksToBounds = YES;
        [self.ageBtn setTitleColor:Theme_Color_White forState:UIControlStateNormal];
        self.ageBtn .imageEdgeInsets = UIEdgeInsetsMake(0,-5,0,0);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
        self.ageBtn.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
        [_coverView addSubview:_ageBtn];
        
     
        _constellationLab = [[UILabel alloc] init];
        _constellationLab.textColor = Theme_Color_White;
        _constellationLab.layer.cornerRadius = 4;
        _constellationLab.layer.masksToBounds = YES;
        _constellationLab.backgroundColor = RandomColor;
        _constellationLab.textAlignment = NSTextAlignmentCenter;
        [_coverView addSubview:_constellationLab];
        
        self.constellationLab.font = FontP(12);
        self.ageBtn.titleLabel.font = FontBold(12);
        
        _addressImg =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder_date"]];
        [_coverView addSubview:_addressImg];
        
      
        
        _address = [[UILabel alloc] init];
        _address.textAlignment = NSTextAlignmentLeft;
        _address.textColor = RGB(110, 110, 110);
        _address.font = FontP(13);
        [_coverView addSubview:_address];
     
        _deteleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deteleBtn addTarget:self action:@selector(removeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_deteleBtn setImage:[UIImage imageNamed:@"p_center_dtxq_delete_button"] forState:UIControlStateNormal];
        
        [self.imageView addSubview:_deteleBtn];
        
        _messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_messageBtn addTarget:self action:@selector(indexBannerSubiew:MessageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_messageBtn setImage:[UIImage imageNamed:@"viedo_message_icon"] forState:UIControlStateNormal];
        
        [self.imageView addSubview:_messageBtn];
        
        
        

    }
    return self;
}

-(void)removeBtnClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(removeBtnClick:)]) {
        [self.delegate removeBtnClick:sender];
    }
}

- (void)indexBannerSubiew:(XLPhotoCell *)subiew MessageBtnClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(indexBannerSubiew:MessageBtnClick:)]) {
        [self.delegate indexBannerSubiew:self MessageBtnClick:sender];
    }
    

}
-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath =indexPath;
    _deteleBtn.tag = indexPath.row;
    _messageBtn.tag =indexPath.row;
}
-(void)setModel:(CheekVodeModel *)model{
    
    self.layer.shadowColor=[UIColor grayColor].CGColor;
    self.layer.shadowOffset=CGSizeMake(1, 1);
    self.layer.shadowOpacity=0.5;
    self.layer.shadowRadius=8;

    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView).offset(-75*SCREEN_WIDTH_RATIO47);
            make.left.right.equalTo(self.contentView);
        }];
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.photo)] placeholderImage:[UIImage imageNamed:@"llustration"]];
    
    
   [_deteleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.left.equalTo(self.imageView).offset(15*SCREEN_WIDTH_RATIO47);
       make.width.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
   }];
    
    
    [_messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView).offset(15*SCREEN_WIDTH_RATIO47);
        make.right.equalTo(self.imageView).offset(-15*SCREEN_WIDTH_RATIO47);
        make.width.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
    }];
    
    
    
    
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.equalTo(self.imageView.mas_bottom);
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(75*SCREEN_WIDTH_RATIO47);
    }];


    [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coverView).offset(15*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_coverView).offset(15*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
    }];

    [_ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLab.mas_right).offset(10*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_coverView).offset(17*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
        
    }];
    
    [_constellationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_ageBtn.mas_right).offset(5*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_coverView).offset(17*SCREEN_WIDTH_RATIO47);
        
        make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        
    }];

    [_addressImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coverView).offset(15*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_nameLab.mas_bottom).offset(10*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(15*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(15*SCREEN_WIDTH_RATIO47);
    }];
    
    [_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_addressImg.mas_right).offset(5*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(_nameLab.mas_bottom).offset(5*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(25*SCREEN_WIDTH_RATIO47);
    }];

    
    self.nameLab.text = model.nickName;
    
    if (model.sex == 0) {
        [self.ageBtn setBackgroundColor:ColorI(0xf28484)];
        
        [self.ageBtn setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    }else{
        [self.ageBtn setBackgroundColor:ColorI(0x84b9f2)];
        
        [self.ageBtn setImage:[UIImage imageNamed:@"male"] forState:UIControlStateNormal];
    }
    //年龄
    [self.ageBtn setTitle:model.age  forState:UIControlStateNormal];
    //星座
    self.constellationLab.backgroundColor = [self getColorWithStr:model.star];
    
    self.constellationLab.text = model.star;
    
    self.address.text = model.location;
    
}
- (UIColor *)getColorWithStr:(NSString *)starStr{
    
    NSArray * starArr = @[@"白羊座",@"天秤座",@"金牛座",@"天蝎座",@"双子座",@"射手座",@"巨蟹座",@"摩羯座",@"狮子座",@"处女座",@"双鱼座"];
    NSArray * colorArr = @[ColorI(0xd80808),ColorI(0xec3db0),ColorI(0x2898df),ColorI(0xa651c9),ColorI(0xff6839),ColorI(0x77ca69),ColorI(0xd16739),ColorI(0x30d3d5),ColorI(0x19d7ad),ColorI(0xecac3d),ColorI(0x32b576)];
    
    
    for (NSString * star in starArr) {
        if ([star isEqualToString:starStr]) {
            NSInteger index = [starArr indexOfObject:star];
            if (colorArr[index]) {
                return colorArr[index];
            }
        }
    }
    return ColorI(0xd80808);
}

@end
